package com.hyh.fireworks.web.admin;

import com.hyh.fireworks.common.result.AjaxResult;
import com.hyh.fireworks.dto.UserRegisterParam;
import com.hyh.fireworks.entity.User;
import com.hyh.fireworks.service.IUserService;
import com.hyh.fireworks.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Summerday
 */

@RequestMapping("/admin")
@Api(tags = "后台用户管理系统")
@RestController
public class AdminController extends BaseController {

    @Resource
    IUserService userService;

    @PostMapping("/login")
    @ApiOperation("登录")
    public AjaxResult login(String username, String password) {
        User user = userService.login(username, password);
        if (user == null) {
            return AjaxResult.error("用户名或密码错误, 登录失败!");
        }
        user.setPassword(null);
        return AjaxResult.ok("登录成功", user);
    }

    @PostMapping("/register")
    @ApiOperation("注册")
    public AjaxResult register(@RequestBody UserRegisterParam registerParam){
        if(!registerParam.getPassword().equals(registerParam.getRePassword())){
            return AjaxResult.error("请确认两次输入的密码是否一致!");
        }
        User register = userService.register(registerParam);
        return register != null ? AjaxResult.ok("注册成功!"):AjaxResult.error("注册失败!");
    }

}
